package com.beecode.enzyme.abrazos.core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.json.JSONArray;
import org.json.JSONObject;

public class TargetCollector {
	
	public static final TargetCollector INSTANCE = new TargetCollector();
	
	public Map<IPath, List> targetBundles = new HashMap<IPath, List>();
	
	private TargetCollector(){
	}

	public void initTargetBundles(IWorkspace workspace){
		synchronized (targetBundles) {
			IPath location = workspace.getRoot().getLocation();
			List<String> list = targetBundles.get(location);
			if (list == null) {
				list = new ArrayList<String>();
				String folder = location.toOSString() + File.separator + "target" + File.separator + "frontend";
				collectBundles(list, folder);
				targetBundles.put(location, list);
			}
		}
	}

	public void collectBundles(List list, String folder) {
		File targetFolder = new File(folder);
		File[] goods = targetFolder.listFiles();
		for (int i = 0; i < goods.length; i++) {
			if(goods[i].isDirectory()){
				File[] bundles = goods[i].listFiles();
				for (int j = 0; j < bundles.length; j++) {
					if(bundles[j].isDirectory()){
						list.add(bundles[j].getAbsolutePath());
					}
				}
			}
		}
	}
	
	public void removeTargetBundles(IWorkspace workspace){
		IPath workspacePath = workspace.getRoot().getLocation();
		synchronized (targetBundles) {
			targetBundles.remove(workspacePath);
		}
	}
	
	public static void main(String[] args) {
		TargetCollector.INSTANCE.collectBundles(new ArrayList<>(), "/home/z/work/paasdb/workspaces/zhengjiachao/test/target/frontend");
	}
}
